from gameServer import gameServer
from eventServer import eventServer
from rCube import RCube
from vec2 import Vec2
from vec3 import Vec3
from soundServer import sound

class Bullet(RCube):
    
    def __init__(self, player, pos, direction):
        RCube.__init__(self)
        
        self.setVar("texture", "bullet")
        self.setVar("player", player)
        self.speed = 20
        if(direction == "left"):
            self.setVar("vel", Vec2(-self.speed, 0))
            self.setVar("pos", Vec2(pos.x - 1, pos.y))
        else:
            self.setVar("vel", Vec2(self.speed, 0))
            self.setVar("pos", Vec2(pos.x + 1, pos.y))
        self.setVar("scale",Vec3(0.5, 0.5, 0.5))
        self.setVar("vulnerable", True)
        self.setVar("lifeTime", 0.5)
        self.setVar("sides",['t','t','t','t'])
        self.setVar("base", "bullet")
        
    def update(self, cmd, args):
        RCube.update(self, cmd, args)
        
        p = self.getVar("pos")
        v = self.getVar("vel")
        dt = args["dtime"]
        
        p.x += (v.x * dt)
        p.y += (v.y * dt)
        
        self.setVar("lifeTime", self.getVar("lifeTime") - dt)
        l = self.getVar("lifeTime")
        
        if(l < 0):
            self.die()
                
    def onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object):
        objectBase = self.getBase(object)
        
        if(objectBase == "player"):
            fromPlayer = self.getVar("player")
            toPlayer = object.getVar("player")
            
            if(not(toPlayer == fromPlayer)):
                print "player %d shot player %d" % (fromPlayer,toPlayer)
                eventServer.pull('playerStomp',{'fromPlayer':fromPlayer,'toPlayer':toPlayer})
                self.die()
                sound.play("gun_kill")
    
    def die(self):
        gameServer.removeObject(self.getVar("name"))